import os
import yaml
from page.webpage import WebPage, sleep
from common.readelement import Element


class LoginPage(WebPage):
    """登录"""

    def input_text_all(self, search_yaml):
        """输入账户"""
        search = Element(search_yaml)
        yaml_path = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
                                 "./page_element/" + search_yaml + ".yaml")
        with open(yaml_path, "r", encoding="utf-8") as f:
            data = yaml.load(f, Loader=yaml.FullLoader)
            lens = len(data) + 1

        try:
            for i, j in zip(range(lens), range(1, lens)):
                reslist = list(data)
                if reslist[i][0:2] == "点击" and reslist[j][0:2] == "文本":
                    sleep()
                    self.is_click(search[reslist[i]])
                    sleep()
                    self.input_text(search[reslist[i]], txt=data[reslist[j]])

                elif reslist[i][0:2] == "点击":
                    sleep()
                    self.is_click(search[reslist[i]])
                    if j == lens - 1:
                        self.is_click(search[reslist[j]])
                        sleep()

                elif reslist[i][0:2] == "图片" and reslist[j][0:2] == "文件":
                    sleep()
                    self.input_file(search[reslist[i]], file=data[reslist[j]])

        except Exception as e:
            print(e)
